<?php

namespace app\admin\model\crm;

use think\Model;


class Order extends Model
{

    

    

    // 表名
    protected $name = 'crm_order';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'status_text'
    ];
    
     protected static function init()
    {
     
        self::beforeInsert(function ($row) {
            if (!isset($row['admin_id']) || !$row['admin_id']) {
                $admin_id = session('admin.id');
                $row['admin_id'] = $admin_id ? $admin_id : 0;
                $row['order_no'] = 'GHD'.date('YmdHis').($admin_id ? $admin_id : 0);
            }
        });
    }

    
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }

    public function getStatusList2()
    {
        return ['0' => __('Status1 0'), '1' => __('Status1 1')];
    }
    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }




    public function admin()
    {
        return $this->belongsTo('app\admin\model\Admin', 'admin_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }


    public function gongyingshang()
    {
        return $this->belongsTo('app\admin\model\pifa\Gongyingshang', 'gongyingshang_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
}
